﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using System.Web.Mvc.Html;
using aspZone.Commerce.Model;
using System.Text;
using System.Configuration;
using aspZone.Commerce.Web.App.Controllers;

namespace aspZone.Commerce.Web.App.Helpers
{
    public static class PaypalCheckoutHelper
    {
        public static string PaypalCheckout(this HtmlHelper helper, Order order)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<form method=\"post\" action=\"{0}\">", FormAction);

            sb.Append(helper.Hidden("cmd", "_cart"));
            sb.Append(helper.Hidden("upload", "1"));
            sb.Append(helper.Hidden("business", MerchantId));
            sb.Append(helper.Hidden("custom", order.Id));
            
            string returnurl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + helper.BuildUrlFromExpression<CartController>(x => x.Index());

            sb.Append(helper.Hidden("return", returnurl));

            int index = 1;
            foreach (var item in order.Items)
            {

                sb.Append(helper.Hidden("item_name_" + index, helper.Encode(item.Product.Name)));
                sb.Append(helper.Hidden("amount_" + index, item.Product.Price.ToString("C")));
                sb.Append(helper.Hidden("quantity_" + index, item.Quantity));
                index++;
            }
            sb.Append(helper.Hidden("shipping", order.ShippingAndHandling));

            sb.Append("<input type=\"image\" src=\"https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif\" />");

            sb.Append("</form>");
            return sb.ToString();
        }

        private static string FormAction
        {
            get
            {
                if (Sandbox)
                {
                    return "https://www.sandbox.paypal.com/cgi-bin/webscr";
                }
                else
                {
                    return "https://www.paypal.com/cgi-bin/webscr";
                }
            }
        }

        private static string MerchantId
        {
            get
            {
                return ConfigurationManager.AppSettings["Paypal.MerchantId"];
            }
        }

        private static bool Sandbox
        {
            get
            {
                bool sandbox = true;
                bool.TryParse(ConfigurationManager.AppSettings["Paypal.SandBoxMode"], out sandbox);
                return sandbox;
            }
        }

        public static string GetSiteUrl() {

            if (HttpContext.Current.Request.ApplicationPath == "/") {
                return "/";
            }

          string url = HttpContext.Current.Request.Url.AbsoluteUri;
            int end = url.IndexOf(HttpContext.Current.Request.ApplicationPath) + 
                  HttpContext.Current.Request.ApplicationPath.Length;
            url = url.Substring(0, end);
            return url;
        }


    }
}
